package com.pro.addresslist.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.pro.addresslist.R;
import com.pro.addresslist.dal.bll.IPMACBLL;
import com.pro.addresslist.dal.model.IPMAC;
import com.pro.addresslist.ui.activity.MacAddressActivity.ReciveBroadcast;
import com.pro.addresslist.ui.adapter.CommonAdapter;
import com.pro.addresslist.ui.adapter.ViewHolder;
import com.pro.addresslist.ui.common.BaseActivity;

public class ShowMacActivity extends BaseActivity {
	private  TextView  phonemac;
	private TextView phonename;
	private ListView macaddress;
	 private  CommonAdapter<IPMAC> macAddress;
    private  List<IPMAC> macaddress2=new ArrayList<IPMAC>();
    private  List<IPMAC> macaddress1=new ArrayList<IPMAC>();
   private List<IPMAC> phoneInfo=new ArrayList<IPMAC>();
   private  IPMAC mac=new IPMAC();
 private  void init(){
	 macaddress=(ListView) findViewById(R.id.list_mac);
	 
 }
	@Override
	public int getLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_mac;
	}
	//接收Mac地址的广播
		public class ReciveBroadcast extends BroadcastReceiver {

			@SuppressLint("NewApi") @Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				String  action=intent.getAction();
				if(action=="lbs_SelectPhone"){
					
					//得到广播中得到的数据，并显示出来 
					 macaddress1= (List<IPMAC>)intent.getSerializableExtra("parameter");
		    		for(IPMAC mac: macaddress1){
		    			
		    			macaddress2.add(mac);
		    		}
		    		 macAddress.notifyDataSetChanged();
		    		
		    	}
				if(action=="lbs_AddPhone"){
					String result=(String) intent.getSerializableExtra("result");
					if(result.equals("succeed")){
						
						Toast.makeText(mContext, "绑定成功！！", Toast.LENGTH_SHORT).show();
						Intent  intents=new Intent();
						intents.setClass(mContext, MacAddressActivity.class);
						startActivity(intents);
					}
					else{
						Toast.makeText(mContext, "绑定 失败！！", Toast.LENGTH_SHORT).show();
						
					}
					
				}
		}
		}
	@Override
	public void OnActCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		    init();
		    mac.set_MACorTEL( getMacAddress(mContext));
		    mac.set_Depict(android.os.Build.MODEL);
		    phoneInfo.add(mac);
		 macAddress=new CommonAdapter<IPMAC>(ShowMacActivity.this,  phoneInfo,R.layout.macaddress_item) {
				
				@Override
				public void convert(ViewHolder holder, IPMAC t) {
					// TODO Auto-generated method stub
					holder.setText(R.id.txt_macaddress, t.get_MACorTELEn());
					holder.setText(R.id.txt_phone_type,t.get_Depict() );
				}
			};
			macaddress.setAdapter(macAddress);
			ReciveBroadcast receive=new ReciveBroadcast();
			IntentFilter filter=new IntentFilter();
			filter.addAction("lbs_SelectPhone");
			filter.addAction("lbs_AddPhone");
			registerReceiver(receive, filter);
			IPMACBLL.SelectPhone(mContext);
			macaddress.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long arg3) {
					// TODO Auto-generated method stub
					if(macaddress2.get(position).get_MACorTELEn().equals(phonemac.getText().toString())){
						
						Toast.makeText(mContext, "该手机已经绑定过！！", Toast.LENGTH_SHORT).show();
						Intent  intents=new Intent();
						intents.setClass(mContext, MacAddressActivity.class);
						startActivity(intents);
					}
					else{
						IPMACBLL.AddPhone(mContext, mac);
						
						
					}
				}
			});
	}

	@Override
	public void OnViewClick(View v) {
		// TODO Auto-generated method stub
	
	}
	//获取手机mac 地址
	public static String getMacAddress(Context context) {  
        // 获取mac地址：  
        String macAddress = "000000000000";  
        try {  
            WifiManager wifiMgr = (WifiManager) context  
                    .getSystemService(Context.WIFI_SERVICE);  
            WifiInfo info = (null == wifiMgr ? null : wifiMgr  
                    .getConnectionInfo());  
            if (null != info) {  
                if (!TextUtils.isEmpty(info.getMacAddress()))  
                    macAddress = info.getMacAddress().replace(":", "");  
                else  
                    return macAddress;  
            }  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            return macAddress;  
        }  
        return macAddress;  
    }  
}
